<template>
  <div>
    <!-- 面包屑导航 -->
    <tab-control :title="['首页','商品管理','商品分类']"></tab-control>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 分配表格 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        class="treeTable"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delCate(scope.row.cat_id)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      width="50%"
      @close="addCateClose"
    >
      <el-form
        ref="addCateFormRef"
        :model="addCateForm"
        :rules="addCateFormRules"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <!-- :options指定数据源 -->
          <el-cascader
            v-model="selectKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editCateDialog"
      width="50%"
      @close="editDialogClose"
    >
      <el-form
        ref="editCateFormRef"
        :model="editCateForm"
        :rules="editCateFormRules"
        label-width="100px"
      >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialog = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import TabControl from '@/components/tabcontrol/TabControl.vue'
import { getCateList, getParentCateList, addCate, editCate,delCate } from "@/api/utils";
export default {
  name: "categories",
  components:{
    TabControl
  },
  data() {
    return {
      catelist: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      columns: [
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", type: "template", template: "isok" },
        { label: "排序", type: "template", template: "order" },
        { label: "操作", type: "template", template: "opt" },
      ],
      addCateDialog: false,
      editCateDialog: false,
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      editCateForm: {
        cat_name: "",
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      editCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      parentCateList: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
        expandTrigger: "hover",
      },
      selectKeys: [],
      editFormId: ""
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    //获取商品分类数据
    getCateList() {
      getCateList(this.queryInfo).then((res) => {
        // console.log(res);
        if (res.meta.status != 200)
          return this.$message.error("获取商品分类失败!");
        this.catelist = res.data.result;
        this.total = res.data.total;
      });
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    showCateDialog() {
      this.addCateDialog = true;
      this.getParentCateList();
    },
    //获取父级分类的数据列表
    getParentCateList() {
      getParentCateList().then((res) => {
        // console.log(res);
        if (res.meta.status != 200)
          return this.$message.error("获取父级分类的数据列表失败!");
        this.parentCateList = res.data;
      });
    },
    parentCateChange() {
      console.log(this.selectKeys);
      if (this.selectKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1];
        this.addCateForm.cat_level = this.selectKeys.length;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },

    //添加分类
    addCate() {
      // console.log(this.addCateForm);
      this.$refs.addCateFormRef.validate((valid) => {
        if (!valid) return;
        addCate(this.addCateForm).then((res) => {
          // console.log(res);
          if (res.meta.status != 201)
            return this.$message.error("添加分类失败!");
          this.$message.success("添加分类成功!");
          this.getCateList();
          this.addCateDialog = false;
        });
      });
    },
    //取消
    addCateClose() {
      this.$refs.addCateFormRef.resetFields();
      this.selectKeys = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
    },
    //关闭编辑对话框
    editDialogClose() {
      this.$refs.editCateFormRef.resetFields();
      this.editCateForm.cat_name = "";
    },
    //打开编辑对话框
    showEditDialog(editInfo) {
      // console.log(editInfo);
      this.editCateDialog = true;
      this.editFormId = editInfo.cat_id;
      this.editCateForm.cat_name = editInfo.cat_name;
    },
    //修改分类
    editCate() {
      editCate(this.editFormId, this.editCateForm.cat_name).then((res) => {
        // console.log(res);
        if (res.meta.status != 200) return this.$message.error("修改失败!");
        this.$message.success("修改成功!");
        (this.editFormId = ""), (this.editCateForm.cat_name = "");
        this.editCateDialog = false;
        this.getCateList();
      });
    },
    //删除分类
    delCate(id) {
      // console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delCate(id).then(res=>{
            // console.log(res);
            if(res.meta.status!=200) return this.$message.error("删除失败!");
            this.$message.success("删除成功!");
            this.getCateList()
          })
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>